<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>影院管理</el-breadcrumb-item>
      <el-breadcrumb-item>影院列表</el-breadcrumb-item>
      <el-breadcrumb-item>放映厅列表</el-breadcrumb-item>
      <el-breadcrumb-item>新增排片计划</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 分割线 -->
    <el-divider></el-divider>
    <p>
      为<span style="color: #409eff; font-weight: bold">{{
        this.cinemaInfo.cinema_name
      }}</span
      >影院的<span style="color: #409eff; font-weight: bold"
        >{{ this.cinemaInfo.cinema_room_name }}({{
          this.cinemaInfo.cinema_room_type
        }})</span
      >
      添加排片
    </p>
    <el-divider></el-divider>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="选择电影" prop="movie_id">
        <el-select
          ref="movieSelector"
          style="width: 300px"
          v-model="form.movie_id"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in movieList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="放映日期">
        <el-date-picker
          style="width: 150px"
          value-format="yyyy-MM-dd"
          prop="showingon_date"
          v-model="form.showingon_date"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
        &nbsp;
        <el-time-select
          style="width: 140px"
          :picker-options="{
            start: '00:00',
            step: '00:30',
            end: '24:00',
          }"
          v-model="form.showingon_time"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="票价" prop="price">
        <el-input style="width: 300px" v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="立即发布" prop="status">
        <el-switch
          active-value="1"
          inactive-value="0"
          style="width: 300px"
          v-model="form.status"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即新增</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        movie_id: "",
        cinema_id: "",
        cinema_room_id: this.$route.params.id,
        price: 0,
        status: false,
        showingon_date: "",
        showingon_time: "",
      },
      rules: {
        cinema_id: [
          { required: true, message: "请填写影院id", trigger: "blur" },
        ],
        movie_id: [
          { required: true, message: "请填写电影id", trigger: "blur" },
        ],

        cinema_room_id: [
          { required: true, message: "请填写放映厅id", trigger: "blur" },
        ],
        price: [
          { required: true, message: "请填写价格", trigger: "blur" },
          {
            parttern: /\d+(.\d+)?/,
            message: "请填写正确的格式",
            trigger: "blur",
          },
        ],
        status: [
          { required: true, message: "请填写是否发布", trigger: "blur" },
        ],
        showingon_date: [
          { required: true, message: "请填写日期", trigger: "blur" },
        ],
        showingon_time: [
          { required: true, message: "请填写场次", trigger: "blur" },
        ],
      },
      // 存储当前电影列表
      movieList: {},
      // 存储当前放映厅信息
      cinemaInfo: {},
      // 下拉框是否正在加载
      loading: false,
    }
  },
  methods: {
    // 点击提交
    onSubmit() {
      this.form.cinema_id = this.cinemaInfo.cinema_id
      // 通过验证表单 发送请求
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$http.ShowingonPlanApi.add(this.form).then((res) => {
            console.log(res)
            // 获取电影名称
            let movie_name = this.$refs.movieSelector.selectedLabel
            console.log("movieSelector", this.$refs.movieSelector)
            if (res.data.code == 200) {
              this.$notify({
                title: "添加排片成功",
                message: `${this.cinemaInfo.cinema_name}影院的${this.cinemaInfo.cinema_room_name}将会在${this.form.showingon_date} ${this.form.showingon_time}放映《${movie_name}》`,
                type: "success",
              })
              this.$router.go(-1)
            }
          })
        } else {
          this.$message.warning("请完善表单")
        }
      })
    },
    // 当输入电影关键字后执行
    remoteMethod(query) {
      if (!query) {
        return
      }
      // 模糊查询
      this.$http.MovieApi.listByName({
        name: query,
        page: 1,
        pagesize: 20,
      }).then((res) => {
        this.movieList = res.data.data.result
        console.log("电影数据", this.movieList)
      })
    },
    // 通过id加载影院放映厅列表
    loadCurrentCinemaRooms() {
      this.$http.CinemaRoomApi.queryById({
        id: this.form.cinema_room_id,
      }).then((res) => {
        console.log("当前影院放映厅", res)
        this.cinemaInfo = res.data.data
      })
    },
  },
  mounted() {
    // 加载当前放映厅信息
    this.loadCurrentCinemaRooms()
  },
}
</script>
